package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.House;
import com.ruoyi.system.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房源Controller
 *
 * @author ...
 * @date 2022-03-18
 */
@RestController
@RequestMapping("/system/house")
public class HouseController extends BaseController {
    @Autowired
    private IHouseService houseService;

    /**
     * 查询房源列表
     */
    @GetMapping("/list")
    public TableDataInfo list(House house) {
        startPage();
        List<House> list = houseService.selectHouseList(house);
        return getDataTable(list);
    }

    /**
     * 获取房源详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(houseService.selectHouseById(id));
    }

    /**
     * 新增房源
     */
    @PostMapping
    public AjaxResult add(@RequestBody House house) {
        house.setCreateUser(SecurityUtils.getUserId().intValue());
        house.setCreateTime(DateUtils.getNowDate());
        return toAjax(houseService.insertHouse(house));
    }

    /**
     * 修改房源
     */
    @PutMapping
    public AjaxResult edit(@RequestBody House house) {
        house.setUpdateUser(SecurityUtils.getUserId().intValue());
        house.setUpdateTime(DateUtils.getNowDate());
        return toAjax(houseService.updateHouse(house));
    }

    /**
     * 删除房源
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(houseService.deleteHouseByIds(ids));
    }
}
